package com.LeeCode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 奇偶树
 */

public class Code1609 {
    public static void main(String[] args) {
        Integer[] arr = {2, 12, 8, 5, 9, null, null, 18, 16};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code1609().isEvenOddTree(root));
    }

    public boolean isEvenOddTree(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        boolean flag = false;

        while (!queue.isEmpty()) {
            int size = queue.size(), preNum = flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);

                if (flag) {
                    if (preNum <= node.val || (node.val & 1) == 1) return false;
                } else {
                    if (preNum >= node.val || (node.val & 1) == 0) return false;
                }
                preNum = node.val;
            }
            flag = !flag;
        }

        return true;
    }
}
